﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Xml;
using DongDong.Common;

public partial class GRZX_Default : Base
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string Operate = Convert.ToString(Request["Operate"]);
            int NavaRootID = Misc.GetQueryStringAsInt("RootID");
            //判断是否是ajax操作
            if (!string.IsNullOrEmpty(Operate))
            {
                Response.Clear();
                if (Operate == "GetNodes")
                {
                    switch (NavaRootID)
                    {
                        case (int)navasort.教学中心:
                            LoadWSXY();
                            break;
                        default:
                            LoadWSXY();
                            break;
                    }
                }
                else if (Operate == "GetKeyNava")
                {
                    switch (NavaRootID)
                    {
                        case (int)navasort.教学中心:
                            LoadWSXYKJNAVA();
                            break;
                        default:
                            LoadWSXYKJNAVA();
                            break;
                    }

                }
                else if (Operate == "ChangNava")
                {
                    ChangeNava();
                }
                else if (Operate == "GetHome")
                {
                    LoadHomePage();
                }
                Response.End();
            }
        }
    }


    public void LoadWSXY()
    {
        Client objClient = Client.GetCurrent();
        if (objClient == null)
        {
            Response.Write("[]");
            return;
        }
        YHDR_CDSX_KJDH objYHDR_CDSX_KJDH = YHDR_CDSX_KJDH.GetByAddUserID(objClient.ID);
        DataTable Nodes = LoadXML2("./NavaData/WXXY.xml");
        if (objYHDR_CDSX_KJDH != null && !string.IsNullOrEmpty(objYHDR_CDSX_KJDH.CDSX))
        {
            string WXXY = objYHDR_CDSX_KJDH.CDSX.Split(';')[0];
            if (!string.IsNullOrEmpty(WXXY))
            {
                Response.Write("[");
                string[] Navas = WXXY.Split('_');
                DataRow[] drNavas;
                for (int i = 0; i < Navas.Length; i++)
                {
                    string rootPId = Navas[i];
                    drNavas = Nodes.Select("rootPId='" + rootPId + "'");
                    if ((i + 1) != Navas.Length)
                        Response.Write(JsonBodyReturn(drNavas) + ",");
                    else
                        Response.Write(JsonBodyReturn(drNavas));
                }
                Response.Write("]");
            }
            else
            {
                if (Nodes != null)
                    Response.Write("[" + JsonBodyReturn(Nodes) + "]");
            }
        }
        else
        {
            if (Nodes != null)
                Response.Write("[" + JsonBodyReturn(Nodes) + "]");
        }
    }

    public void LoadWSXYKJNAVA()
    {
        string StrFrmat = "";
        Client objClient = Client.GetCurrent();
        if (objClient == null)
        {
            Response.Write("请先登入");
            return;
        }
        YHDR_CDSX_KJDH objYHDR_CDSX_KJDH = YHDR_CDSX_KJDH.GetByAddUserID(objClient.ID);
        DataTable Nodes = LoadXML2("./NavaData/WXXY.xml");
        DataRow[] drNavas = Nodes.Select("pId=0");
        StringBuilder SortNava = new StringBuilder();
        if (objYHDR_CDSX_KJDH != null && !string.IsNullOrEmpty(objYHDR_CDSX_KJDH.CDSX))
        {
            string WXXY = objYHDR_CDSX_KJDH.CDSX.Split(';')[0];
            if (string.IsNullOrEmpty(WXXY))
            {
                for (int i = 0; i < drNavas.Length; i++)
                {
                    SortNava.Append("<select>");
                    for (int j = 0; j < drNavas.Length; j++)
                    {
                        SortNava.Append("<option value='" + Convert.ToString(drNavas[j]["id"]) + "'");
                        if (i == j)
                            SortNava.Append(" selected='selected'>");
                        else
                            SortNava.Append(">");
                        SortNava.Append((j + 1) + "</option>");
                    }
                    SortNava.Append("</select>");
                    SortNava.Append("&nbsp;<span id='DHSPAN" + Convert.ToString(drNavas[i]["id"]) + "'>" + Convert.ToString(drNavas[i]["name"]) + "</span><br />");
                }
            }
            else
            {
                string[] Navas = WXXY.Split('_');
                for (int i = 0; i < Navas.Length; i++)
                {
                    SortNava.Append("<select>");
                    DataRow DataRowText;
                    for (int j = 0; j < Navas.Length; j++)
                    {

                        SortNava.Append("<option value='" + Convert.ToString(Navas[j]) + "'");
                        if (i == j)
                            SortNava.Append(" selected='selected'>");
                        else
                            SortNava.Append(">");
                        StrFrmat = string.Format("id='{0}'", Convert.ToString(Navas[j]));
                        DataRowText = Nodes.Select(StrFrmat)[0];
                        SortNava.Append((j + 1) + "</option>");
                    }
                    StrFrmat = string.Format("id='{0}'", Convert.ToString(Navas[i]));
                    DataRowText = Nodes.Select(StrFrmat)[0];
                    SortNava.Append("</select>");
                    SortNava.Append("&nbsp;<span id='DHSPAN" + Convert.ToString(Convert.ToString(Navas[i])) + "'>" + Convert.ToString(DataRowText["name"]) + "</span><br />");
                }
            }
        }
        else
        {
            for (int i = 0; i < drNavas.Length; i++)
            {
                SortNava.Append("<select>");
                for (int j = 0; j < drNavas.Length; j++)
                {
                    SortNava.Append("<option value='" + Convert.ToString(drNavas[j]["id"]) + "'");
                    if (i == j)
                        SortNava.Append(" selected='selected'>");
                    else
                        SortNava.Append(">");
                    SortNava.Append((j + 1) + "</option>");
                }
                SortNava.Append("</select>");
                SortNava.Append("&nbsp;<span id='DHSPAN" + Convert.ToString(drNavas[i]["id"]) + "'>" + Convert.ToString(drNavas[i]["name"]) + "</span><br />");
            }
        }
        Response.Write(SortNava.ToString());
    }
    public void LoadHomePage()
    {
        Client objClient = Client.GetCurrent();
        if (objClient != null)
        {
            YHDR_CDSX_KJDH objYHDR_CDSX_KJDH = YHDR_CDSX_KJDH.GetByAddUserID(objClient.ID);
            if (objYHDR_CDSX_KJDH != null)
            {
                string[] Navas = objYHDR_CDSX_KJDH.CDSX.Split(';');
                if (Navas.Length == 4 && !string.IsNullOrEmpty(Navas[3]))
                {
                    Response.Write(Navas[3]);
                }
                else
                {
                    Response.Write("1");
                }

            }
            else
            {
                Response.Write("1");
            }
        }
        else
        {
            Response.Write("1");
        }
    }

    public void ChangeNava()
    {
        Client objClient = Client.GetCurrent();
        if (objClient == null)
        {
            Response.Write("请重新登入");
            return;
        }
        string NavaSort = Server.UrlDecode(Request["Nava"]);
        YHDR_CDSX_KJDH objYHDR_CDSX_KJDH = YHDR_CDSX_KJDH.GetByAddUserID(objClient.ID);
        if (objYHDR_CDSX_KJDH == null)
        {
            objYHDR_CDSX_KJDH = new YHDR_CDSX_KJDH();
            objYHDR_CDSX_KJDH.CDSX = NavaSort + ";;;1";
            objYHDR_CDSX_KJDH.UserID = objClient.ID;
            objYHDR_CDSX_KJDH.LastLoginTime = System.DateTime.Now;
            objYHDR_CDSX_KJDH.KJDH = "";
            objYHDR_CDSX_KJDH.Add();
        }
        else
        {
            string[] Navas = objYHDR_CDSX_KJDH.CDSX.Split(';');
            objYHDR_CDSX_KJDH.CDSX = NavaSort + ";" + Navas[1] + ";" + Navas[2] + ";" + Navas[3];
            objYHDR_CDSX_KJDH.Update();
        }
        Response.Write("success");
    }

    public DataTable LoadXML2(string path)
    {
        try
        {
            DataTable dt = new DataTable();
            DataSet ds = new DataSet();
            //读取XML到DataTable
            ds.ReadXml(GetXmlFullPath(path));
            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
            }
            return dt;
        }
        catch (Exception)
        {
            return null;
        }
    }


    #region GetXmlFullPath
    ///
    /// 返回完整路径
    ///
    /// Xml的路径
    ///
    public static string GetXmlFullPath(string strPath)
    {
        //返回完整路径
        return System.Web.HttpContext.Current.Server.MapPath(strPath);

    }
    #endregion
    /// 返回Json字符串
    /// </summary>
    /// <param name="dt">DataRow对象</param>
    /// <returns>Json字符串</returns>
    public string JsonBodyReturn(DataRow[] dts)
    {
        StringBuilder sb = new StringBuilder();
        //JavaScriptSerializer serializer = new JavaScriptSerializer();

        foreach (DataRow row in dts)
        {
            sb.Append("{");
            int RowCount = row.ItemArray.Count();
            for (int i = 0; i < RowCount; i++)
            {
                sb.Append("\"" + row.Table.Columns[i].ColumnName + "\":\"" + ConvertToJsonString(row.ItemArray[i].ToString()) + "\"");
                sb.Append(",");
            }
            sb = sb.Remove(sb.Length - 1, 1);
            sb.Append("},");
        }
        if (sb.Length != 0)
        {
            sb = sb.Remove(sb.Length - 1, 1);
        }
        return sb.ToString();
    }

    /// 返回Json字符串
    /// </summary>
    /// <param name="dt">DataTable对象</param>
    /// <returns>Json字符串</returns>
    public string JsonBodyReturn(DataTable dt)
    {
        StringBuilder sb = new StringBuilder();
        //JavaScriptSerializer serializer = new JavaScriptSerializer();

        for (int row = 0; row < dt.Rows.Count; row++)
        {
            if (row > 0)
            {
                sb.Append(",");
            }

            sb.Append("{");

            for (int col = 0; col < dt.Columns.Count; col++)
            {
                sb.Append("\"" + dt.Columns[col].ToString() + "\":\"" + ConvertToJsonString(dt.Rows[row][col].ToString()) + "\"");
                if (col < dt.Columns.Count - 1)
                {
                    sb.Append(",");
                }
            }
            sb.Append("}");
        }

        return sb.ToString();
    }

    public string ConvertToJsonString(string input)
    {
        if (string.IsNullOrEmpty(input))
            return input;
        input = input.Replace("\\", "\\\\");
        input = input.Replace("\"", "\\\"");
        //input = input.Replace("/", "\\/");
        input = input.Replace("\b", "\\b");
        input = input.Replace("\t", "\\t");
        input = input.Replace("\f", "\\f");
        input = input.Replace("\n", "\\n");
        input = input.Replace("\r", "\\r");
        return input;
    }
}